Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- analog 2 pwm
- PB0 pwm channel 1
- PB1 pwm channel 2
- PB2
- PB3 Analog 2 in
- PB4 Analog 1 in
- PB5 (RESET)
- CODE BY RUE_MOHR!
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include "avrcommon.h"
- #define OUTPUT 1
- #define INPUT 0
- void PWMInit ( void );
- void Init_ADC( void );
- volatile unsigned int AdcValues[2];
- volatile unsigned char flag;
- int main( void ) {
- // Set clock prescaler: 0 gives full 4.8 MHz from internal oscillator.
- CLKPR = (1 << CLKPCE);
- CLKPR = 0;
- DDRB = ((OUTPUT << PB0) | (OUTPUT << PB1) |(INPUT << PB2) |(INPUT << PB3) |(INPUT << PB4) |(INPUT << PB5 ));
- PWMInit();
- Init_ADC();
- flag = 0;
- sei();
- while(1) {
- NOP();
- }
- }
- //---------------------------------------------------------------------------
- void Init_ADC( void ) {
- // VCC voltager ref, ADC2 (PB4) as default input
- ADMUX = 2| (1<<ADLAR);
- // Enable ADC, ADC prescaler /32
- ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0) | (1<<ADIE);
- // Start conversion and wait for it to complete (result not used)
- ADCSRA |= (1<<ADSC); // Start convertion
- }
- ISR(ADC_vect) {
- char i;
- unsigned char v;
- i = ADMUX & 1; // what channel we on?
- v = ADCH;
- if (v > 0xF7) v = 0xFF;
- if (v < 0x07) v = 0x00;
- if (i == 0) OCR0A = v; // save value
- else OCR0B = v;
- ADMUX = ((1 - i) + 2) | (1<<ADLAR); // select channel
- ADCSRA |= _BV(ADSC); // start next conversion
- flag = 1;
- return;
- }
- void PWMInit() {
- // clear pwm levels
- OCR0A = 0;
- OCR0B = 0;
- // set up WGM, clock, and mode for timer 0
- TCCR0A = 1 << COM0A1 |
- 0 << COM0A0 |
- 1 << COM0B1 |
- 0 << COM0B0 |
- 1 << WGM00 |
- 1 << WGM01 ;
- // set up WGM, clock, and mode for timer 2
- TCCR0B = 0 << CS02 |
- 1 << CS01 |
- 1 << CS00 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement